﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cocon90.Lib.Web
{
    /// <summary>
    /// Json辅助类
    /// </summary>
    public class jsonHelper
    {
        /// <summary>
        /// 将一个DataTable序列化为Json对象放到row中，并添加total属性。
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="total">此次查询记录（假若不分页）的总条数，些属性通常用于客户端分页控件使用。</param>
        /// <returns>Json对象</returns>
        public static object JsonTable(System.Data.DataTable dt, int total)
        {

            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            StringBuilder JsonString = new StringBuilder();
            if (dt != null && dt.Rows.Count > 0)
            {
                JsonString.Append("{");
                JsonString.Append("\"total\":" + total + ",\"rows\":[ ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
                        }
                    }
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }
                JsonString.Append("]}");
                return js.DeserializeObject(JsonString.ToString()); ;
            }
            else
            {
                JsonString.Append("{total:" + total + ",rows:[]}");
                return js.DeserializeObject(JsonString.ToString()); ;
            }
        }
        /// <summary>
        /// 将一个对象进行序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string jsonObject(object obj)
        {
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            return js.Serialize(obj);
        }
        /// <summary>
        ///将一串Json格式的字符串进行序列化为Json
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public static string jsonString(string jsonString)
        {
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            var obj = js.DeserializeObject(jsonString);
            return jsonObject(obj);
        }
    }
}